package com.jet.utils.httpsqs;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.URL;

/**
 * Httpsqs4j基础类，用于设置连接信息及创建客户端对象
 *
 * @author Henry Young
 */
public class Httpsqs4j {

    private String prefix;

    private String charset;

    private boolean configured = false;

    /**
     * 设置连接信息
     *
     * @param ip string
     * @param port int
     * @param charset 字符集
     * @throws HttpsqsException
     */
    public void setConnectionInfo(String ip, int port, String charset) throws HttpsqsException {
        try {
            //noinspection ResultOfMethodCallIgnored
            "".getBytes(charset);
        } catch (UnsupportedEncodingException e) {
            throw new HttpsqsException("Unknown charset.", e);
        }
        URL url;
        HttpURLConnection connection = null;
        String prefix = "http://" + ip + ":" + port + "/";
        try {
            url = new URL(prefix);
            connection = (HttpURLConnection) url.openConnection();
            connection.connect();
        } catch (IOException e) {
            throw new HttpsqsException(prefix + " cannot located.", e);
        } finally {
            if (connection != null) {
                connection.disconnect();
            }
        }
        this.prefix = prefix + "?";
        this.charset = charset;
        configured = true;
    }

    /**
     * 设置带认证连接
     *
     * @param ip string
     * @param port int
     * @param charset 字符集
     * @throws HttpsqsException
     */
    public void setConnectionInfo(String ip, int port, String password, String charset) throws HttpsqsException {
        try {
            //noinspection ResultOfMethodCallIgnored
            "".getBytes(charset);
        } catch (UnsupportedEncodingException e) {
            throw new HttpsqsException("Unknown charset.", e);
        }
        URL url;
        HttpURLConnection connection = null;
        String prefix = "http://" + ip + ":" + port + "/?auth=" + password;
        try {
            url = new URL(prefix);
            connection = (HttpURLConnection) url.openConnection();
            connection.connect();
        } catch (IOException e) {
            throw new HttpsqsException(prefix + " cannot located.", e);
        } finally {
            if (connection != null) {
                connection.disconnect();
            }
        }
        this.setPrefix(prefix);
        this.setCharset(charset);
        this.setConfigured(true);
    }

    /**
     * 创建新的客户端对象
     *
     * @return HttpsqsClient
     */
    public HttpsqsClient createNewClient(Httpsqs4j httpsqs4j) {
        return new HttpsqsClient(httpsqs4j);
    }

    public String getPrefix() {
        return prefix;
    }

    public void setPrefix(String prefix) {
        this.prefix = prefix;
    }

    public String getCharset() {
        return charset;
    }

    public void setCharset(String charset) {
        this.charset = charset;
    }

    public boolean isConfigured() {
        return configured;
    }

    public void setConfigured(boolean configured) {
        this.configured = configured;
    }

}